﻿// <copyright file="MainPage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-08-20</date>
// <summary>MainPage code behind</summary>

namespace WindowsPhoneListApplication
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// List Application main page
    /// </summary>
    public partial class MainPage : PhoneApplicationPage
    {
        /// <summary>
        /// Initializes a new instance of the MainPage class
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// When page is navigated to, set data context 
        /// </summary>
        /// <param name="e">the navigation event argumetns</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Set the data context of the listbox control to the sample data
            if (DataContext == null)
            {
                DataContext = App.ViewModel;
            }
        }

        /// <summary>
        /// Handle selection changed on ListBox
        /// </summary>
        /// <param name="sender">the main list box</param>
        /// <param name="e">the selection changed event arguments</param>
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
            {
                return;
            }

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }
    }
}
